package com.situ.mq.listener;

import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
public class SpringRabbitListener {

    /*@RabbitListener(queues = "basic.queue")
    public void listenBasicQueue(String msg) {
        System.out.println("消费者接收到消息： " + msg);
    }*/

    /*@RabbitListener(queues = "basic.queue")
    public void listenWorkQueue1(String msg) throws InterruptedException {
        System.out.println("消费者1接收到消息： " + msg);
        Thread.sleep(20);
    }

    @RabbitListener(queues = "basic.queue")
    public void listenWorkQueue2(String msg) throws InterruptedException {
        System.err.println("消费者2接收到消息： " + msg);
        Thread.sleep(200);
    }*/

    @RabbitListener(queues = "fanout.queue1")
    public void listenFanoutQueue1(String msg) throws InterruptedException {
        System.out.println("消费者1接收到fanout.queue1消息： " + msg);
    }

    //@RabbitListener(queues = "fanout.queue2")
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue("fanout.queue2"),
            exchange = @Exchange(name = "fanoutExchange", type = ExchangeTypes.FANOUT)
    ))
    public void listenFanoutQueue2(String msg) throws InterruptedException {
        System.out.println("消费者2接收到fanout.queue2消息： " + msg);
    }


    @RabbitListener(bindings = @QueueBinding(
            value = @Queue("direct.queue1"),
            exchange = @Exchange(name = "directExchange", type = ExchangeTypes.DIRECT),
            key = {"blue", "red"}
    ))
    public void listenDirectQueue1(String msg) throws InterruptedException {
        System.out.println("消费者1接收到direct.queue1消息： " + msg);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue("direct.queue2"),
            exchange = @Exchange(name = "directExchange", type = ExchangeTypes.DIRECT),
            key = {"yellow", "red"}
    ))
    public void listenDirectQueue2(String msg) throws InterruptedException {
        System.out.println("消费者2接收到direct.queue2消息： " + msg);
    }

@RabbitListener(bindings = @QueueBinding(
        value = @Queue(name = "topic.queue1"),
        exchange = @Exchange(name = "topicExchange", type = ExchangeTypes.TOPIC),
        key = "china.#"
))
public void listenTopicQueue1(String msg){
    System.out.println("消费者接收到topic.queue1的消息：" + msg);
}

@RabbitListener(bindings = @QueueBinding(
        value = @Queue(name = "topic.queue2"),
        exchange = @Exchange(name = "topicExchange", type = ExchangeTypes.TOPIC),
        key = "japan.#"
))
public void listenTopicQueue2(String msg){
    System.out.println("消费者接收到topic.queue2的消息：" + msg);
}

@RabbitListener(bindings = @QueueBinding(
        value = @Queue(name = "topic.queue3"),
        exchange = @Exchange(name = "topicExchange", type = ExchangeTypes.TOPIC),
        key = "#.weather"
))
public void listenTopicQueue3(String msg){
    System.out.println("消费者接收到topic.queue3的消息：" + msg);
}

@RabbitListener(bindings = @QueueBinding(
        value = @Queue(name = "topic.queue4"),
        exchange = @Exchange(name = "topicExchange", type = ExchangeTypes.TOPIC),
        key = "#.news"
))
public void listenTopicQueue4(String msg){
    System.out.println("消费者接收到topic.queue4的消息：" + msg);
}
}
